package com.example.nbshopping.Buy;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.nbshopping.R;
import com.example.nbshopping.utils.URLUtils;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartGoodsAdapter extends BaseAdapter {
    Context context;
    List<CartGoodsBean.DataBean> data;
    List<Boolean>statuList;
    public CartGoodsAdapter(Context context, List<CartGoodsBean.DataBean> data,List<Boolean> statuList) {
        this.context = context;
        this.data = data;
        this.statuList = statuList;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        VH vh= null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_cart,parent,false);
            vh = new VH(convertView);
            convertView.setTag(vh);
        }else {
            vh = (VH) convertView.getTag();
        }
        CartGoodsBean.DataBean dataBean = data.get(pos);
        vh.titleTv.setText(dataBean.getCommodityName());
        vh.desTv.setText(dataBean.getCommodityInfo());
        vh.priceTv.setText("￥ "+dataBean.getTotalprices()+"元");
        vh.numTv.setText("数量："+dataBean.getCount());
        String photo = dataBean.getCommodityPhoto();
        if (!TextUtils.isEmpty(photo)) {
            String photourl = URLUtils.PUBLIC_URL+photo;
            Picasso.with(context).load(photourl).into(vh.iv);
        }
        final boolean sb = statuList.get(pos);   //查看默认选中状态
        vh.cb.setChecked(sb);
        vh.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                statuList.set(pos,!sb);
                if (selectGoodsListener!=null) {
                    selectGoodsListener.selectGoods(!sb,pos);
                    notifyDataSetChanged();
                }
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public void selectGoodsListener(SelectGoodsListener selectGoodsListener) {
        this.selectGoodsListener = selectGoodsListener;
    }

    public interface SelectGoodsListener{
        void selectGoods(boolean status,int pos);
    }
    SelectGoodsListener selectGoodsListener;

    public void setSelectGoodsListener(SelectGoodsListener selectGoodsListener) {
        this.selectGoodsListener = selectGoodsListener;
    }


    class VH{
        TextView titleTv,priceTv,desTv,numTv;
        ImageView iv;
        CheckBox cb;
        public VH(View v){
            titleTv = v.findViewById(R.id.item_shoppingcar_tv_title);
            priceTv = v.findViewById(R.id.item_shoppingcar_tv_price);
            desTv = v.findViewById(R.id.item_shoppingcar_tv_des);
            iv = v.findViewById(R.id.item_shoppingcar_iv);
            numTv=v.findViewById(R.id.item_shoppingcar_tv_count);
            cb=v.findViewById(R.id.item_checkbox_cb);
        }
    }
}
